package com.cat.model.biz.service.bs.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.core.commons.util.secure.MD5Util;
import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.service.bs.UserBS;
import com.cat.model.common.model.common.UserAuthInfoVO;
import com.cat.model.common.model.common.UserInfoVO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.dao.mapper.UserInfoMapper;

@Service("userBS")
public class UserBSImpl implements UserBS{
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Autowired
	private TxCommonDAO txCommonDAO;
	
	@Override
	public void updateNickName(long userId, String nickName){
		UserInfoVO user = userInfoMapper.getUserInfo(userId);
		if(null == user){
			return;
		}
		
		user.setNickName(nickName);
		
		txCommonDAO.updateById(user, "nickName");
	}
	
	@Override
	public void updateRealName(long userId, String realName){
		UserInfoVO user = userInfoMapper.getUserInfo(userId);
		if(null == user){
			return;
		}
		
		user.setRealName(realName);
		
		txCommonDAO.updateById(user, "realName");
	}
	
	@Override
	public void updateHeadImage(long userId, String headImage){
		UserInfoVO user = userInfoMapper.getUserInfo(userId);
		if(null == user){
			return;
		}
		
		user.setHeadImage(headImage);
		
		txCommonDAO.updateById(user, "headImage");
	}
	
	@Override
	public void updatePassword(long userId, String password){
		UserInfoVO user = userInfoMapper.getUserInfo(userId);
		if(null == user){
			return;
		}
		
		user.setPassword(MD5Util.encode(password));
		
		txCommonDAO.updateById(user, "password");
	}

	@Override
	public void updateBusinessLicense(long userId, String businessLicensePath) {
		UserAuthInfoVO userAuthInfo = modelMapper.getUserAuthInfoByUserId(userId);
		if(null == userAuthInfo){
			return;
		}
		
		userAuthInfo.setBusinessLicense(businessLicensePath);
		userAuthInfo.setApplyTime(new Date());
		userAuthInfo.setApproveStatus("01");
		userAuthInfo.setApproveRemark("待复核");
		
		txCommonDAO.updateById(userAuthInfo, "businessLicense","applyTime","approveStatus","approveRemark");
	}
	
	@Override
	public void updateEmployeeAuthInfo(long userId, String idCardPositivePath,String idCardBackPath,String idCardHolderPath) {
		UserAuthInfoVO userAuthInfo = modelMapper.getUserAuthInfoByUserId(userId);
		if(null == userAuthInfo){
			return;
		}
		
		userAuthInfo.setIdCardPositive(idCardPositivePath);
		userAuthInfo.setIdCardBack(idCardBackPath);
		userAuthInfo.setIdCardHolder(idCardHolderPath);
		userAuthInfo.setApplyTime(new Date());
		userAuthInfo.setApproveStatus("01");
		userAuthInfo.setApproveRemark("待复核");
		
		txCommonDAO.updateById(userAuthInfo, "idCardPositive","idCardBack","idCardHolder","applyTime","approveStatus","approveRemark");
	}
}
